#include "mainwindow.h"
#include"QRect"
#include <ctime>
#include <vector>


extern int playernum;
extern int botnum;

extern std::vector<int> playerTeam;
extern std::vector<int> botTeam;
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{   //
    QFont font;
    font.setPointSize(32);
    paint.setFont(font);
    paint.setPen(QColor(255,0,0));
    //初始发全局结构体
    glo.framei=0;
    glo.status=new Status();
    glo.blockimage=new QImage(":/images/map_block.png");
    glo.tankimage=new QImage(":/images/player_tank.png");
    glo.bulletimage=new QImage(":/images/bullet.png");
    glo.gamemap=new GameMap();
    //初始化player
    for(int i=0;i<playernum;i++)
    {
        glo.players.append(new Tank(qrand()%10,qrand()%17,UP,4,playerTeam[i]));
    }
    //初始化bot
    for(int i=0;i<botnum;i++)
    {
        glo.badtanks.append(new Tank(10,i,UP,2,botTeam[i]));
    }


    //
    gamestatus=gameing;
    laststyle=0;
    leftorright=1;
    timer=new QTimer(this);
    timer->setInterval(25);
    connect(timer,SIGNAL(timeout()),this,SLOT(timefun()));
    timer->start();
}

MainWindow::~MainWindow()
{ 
    timer->stop();
    delete timer;
}
void MainWindow::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    setFixedSize(WIDTH,HEIGHT);

    paint.begin(this);
    glo.gamemap->Display(paint);  
    glo.status->Display(paint);

    for(int i=0;i<glo.players.count();i++)
    {
           glo.players.at(i)->Display(paint);
    }

    for(int i=0;i<glo.badtanks.count();i++)
    {
           glo.badtanks.at(i)->Display(paint);
    }
    paint.end();
}




void MainWindow::keyPressEvent(QKeyEvent *event)
{
    qDebug("key:--------------------------%d ",event->key());
//当按键为M时设置游戏状态为mapedit
    if(event->key()==Qt::Key_M)
    {
        gamestatus=mapedit;
    }
    else if(event->key()==Qt::Key_G)//g返回游戏
    {
        gamestatus=gameing;
    }
    //s为保存地图
    if(gamestatus==mapedit)
    {
        if(event->key()==Qt::Key_S)
        {
             glo.gamemap->savemap("1.dat");
        }
        else if(event->key()==Qt::Key_L)
        {
            glo.gamemap->loadmap("1.dat");
        }

     }
    else if(gamestatus==gameing)
    {
        //player1
        if(glo.players.count()==1)
        {
            if(event->key()==Qt::Key_S)
            {
                glo.players.at(0)->setdir(DOWN);
                glo.players.at(0)->startmove();
                glo.players.at(0)->startfire();
            }
            else if(event->key()==Qt::Key_A)
            {
                glo.players.at(0)->setdir(LEFT);
                glo.players.at(0)->startmove();
                glo.players.at(0)->startfire();
            }
            else if(event->key()==Qt::Key_W)
            {
                glo.players.at(0)->setdir(UP);
                glo.players.at(0)->startmove();
                glo.players.at(0)->startfire();
            }
            else if(event->key()==Qt::Key_D)
            {
                glo.players.at(0)->setdir(RIGHT);
                glo.players.at(0)->startmove();
                glo.players.at(0)->startfire();
            }
            else if(event->key()==Qt::Key_X)
            {
                glo.players.at(0)->startfire();
            }
        }


        //player2
        if(glo.players.count()==2)
        {
            if(event->key()==Qt::Key_Up)
            {
                glo.players.at(1)->setdir(UP);
                glo.players.at(1)->startmove();
                glo.players.at(1)->startfire();
            }
            if(event->key()==Qt::Key_Down)
            {
                glo.players.at(1)->setdir(DOWN);
                glo.players.at(1)->startmove();
                glo.players.at(1)->startfire();
            }
            if(event->key()==Qt::Key_Right)
            {
                glo.players.at(1)->setdir(RIGHT);
                glo.players.at(1)->startmove();
                glo.players.at(1)->startfire();
            }
            if(event->key()==Qt::Key_Left)
            {
                glo.players.at(1)->setdir(LEFT);
                glo.players.at(1)->startmove();
                glo.players.at(1)->startfire();
            }
        }

     }
    update();
}



void MainWindow::keyReleaseEvent(QKeyEvent *event)
{

    if(glo.players.count()==1)
    {
        if(event->key()==Qt::Key_S)
        {
            glo.players.at(0)->stopmove();
            glo.players.at(0)->stopfire();
        }
        else if(event->key()==Qt::Key_A)
        {

            glo.players.at(0)->stopmove();
            glo.players.at(0)->stopfire();
        }
        else if(event->key()==Qt::Key_W)
        {

            glo.players.at(0)->startmove();
            glo.players.at(0)->stopfire();
        }
        else if(event->key()==Qt::Key_D)
        {

            glo.players.at(0)->stopmove();
            glo.players.at(0)->stopfire();
        }
        else if(event->key()==Qt::Key_X)
        {
            glo.players.at(0)->stopfire();
        }
    }



   //player2
   if(glo.players.count()==2)
   {
       if(event->key()==Qt::Key_Up)
       {

           glo.players.at(1)->stopmove();
           glo.players.at(1)->stopfire();
       }
       if(event->key()==Qt::Key_Down)
       {

           glo.players.at(1)->stopmove();
           glo.players.at(1)->stopfire();
       }
       if(event->key()==Qt::Key_Right)
       {

           glo.players.at(1)->stopmove();
           glo.players.at(1)->stopfire();
       }
       if(event->key()==Qt::Key_Left)
       {

           glo.players.at(1)->stopmove();
           glo.players.at(1)->stopfire();
       }
   }


}


// 鼠标移动事件       默认情况下，触发事件需要按下鼠标，才能触发。可设置为自动触发:setMouseTracking(true);
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
//qDebug("left or rigth 1 or 2=》 %d",event->button() );
//qDebug("haha");
    if(gamestatus==mapedit)
    {
        int i,j;//地图块的二维编号地址
        glo.gamemap->cal(event->x(),event->y(),i,j);
        if(leftorright==1)
        {
            //如果移动时候按下左键
            glo.gamemap->setstyle(i,j,laststyle);
        }
        else
        {
            glo.gamemap->deletecell(i,j);
        }
    }//if(gamestatus==mapedit) end
    update();
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    int i,j;//地图块的二维编号地址
    if(gamestatus==mapedit)
    {

        glo.gamemap->cal(event->x(),event->y(),i,j);


        if(event->button()==Qt::LeftButton)
        {
            //qDebug("i %d",i);
            //qDebug("j %d",j);
                leftorright=1;
            laststyle=glo.gamemap->switchstyle(i,j);
        }
        else
        {
            glo.gamemap->deletecell(i,j);
            leftorright=2;
        }
    }
    //if(gamestatus==mapedit) end
    update();
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{

//update();

}

void MainWindow::timefun()
{
    if(gamestatus!=gameing)
    {
        return;
    }
    glo.framei++;
    Dir tmpdir;int tmp;
    /**敌人坦克控制**/
    //qsrand((quint32)time(0)%200);
    for(int i=0;i<glo.badtanks.count();i++)
    {

        if(qrand()%5==0)
        {
            glo.badtanks.at(i)->startmove();
            glo.badtanks.at(i)->startfire();
        }
        if(qrand()%10==0)
        {
            glo.badtanks.at(i)->stopmove();
        }
        if(qrand()%10==0)
        {
            glo.badtanks.at(i)->stopfire();
        }

        if(qrand()%10==0)
        {
            tmp=qrand()%4;
               switch(tmp)
               {
               case 0:
                   tmpdir=UP;
                 break;
               case 1:
                   tmpdir=DOWN;
                 break;
               case 2:
                   tmpdir=LEFT;
                   break;
               case 3:
                   tmpdir=RIGHT;
                   break;
               }
         glo.badtanks.at(i)->setdir(tmpdir);
        }
    }
 /**end**/
    for(int i=0;i<glo.players.count();i++)
    {
        glo.players.at(i)->Move();
        glo.players.at(i)->fire();
    }
    for(int i=0;i<glo.badtanks.count();i++)
    {
        glo.badtanks.at(i)->Move();
        glo.badtanks.at(i)->fire();
    }
    qDebug("timeout");
    update();
}
